﻿package 
{
	import flash.display.*;
	import flash.utils.Dictionary;
	import flash.media.*;
	import flash.text.*;
	import fl.controls.*;
	import flash.events.*;
	import flash.net.*;
	import flash.events.IOErrorEvent;
	import wronglink.vk.*;
	/**
	 * ...
	 * @author kot
	 */
	public class MainVK extends MovieClip {
		public static const LastFM_api_key:String = "0ee7925bc19807741859b31e9a4a6f9e";
		static const VER:String = "2.0";
		static const MY_ID:uint = 3371777; // For local testing please change this to your ID
		static const MY_APP_ID:uint = 1859747; // For local testing please change this to your APP_ID
		static const MY_SECRET:String = "DRZMRx3VlX"; // Please change this to your application secret key
		internal static const TEST_MODE:Boolean = true; // For local testing please change this to "true"
		
		private var LastFM : LastFMParser;
		
		public var txt : TextField;
		public var button : Button;
		
		public var app_id : uint;
		public var api_url:String;
		var api_res:String;
		
		public static var wrapper:* = null;
		public static var STAGE:* = null;
		
		// Flash Vars 
		private var vars:Object = null;
		public var user_id:uint = 0;
		public var viewer_id:uint = 0;
		
		public function MainVK() {
			if (stage) 
				init();
			else 
				addEventListener( Event.ADDED_TO_STAGE, init );
		}
		
		public function init() {
			removeEventListener( Event.ADDED_TO_STAGE, init );
			wrapper = Object(parent.parent);
			if ( wrapper.external == null )
				wrapper = stage; // Local
			if ( wrapper is Stage )	{
				STAGE = wrapper;
				vars = LoaderInfo(parent.loaderInfo).parameters;
			} else {
				STAGE = wrapper.parent;
				vars = wrapper.application.parameters;
			}
			LastFM = new LastFMParser();
			// Read FlashVars
			app_id = getFlashVarInt( "api_id" );
			user_id   = getFlashVarInt( "user_id" );
			viewer_id = getFlashVarInt( "viewer_id" );
			api_url = vars['api_url'];
			api_res = vars['api_result'];
            if ( viewer_id == 0 ) {// Local testing
			    viewer_id = MY_ID;
				user_id = MY_ID;
				app_id = MY_APP_ID;
				api_url = "http://api.vkontakte.ru/api.php";
			}
      		if ( user_id == 0 ) {
				user_id = viewer_id;
			}
			
			//txt field
			txt = new TextField();
			txt.x = 100;
			txt.y = 100;
			txt.text = "Linkin Park";
			txt.border = true;
			txt.height = txt.textHeight * 2;
			txt.type = TextFieldType.INPUT;
			addChild(txt);
			
			//button field
			button = new Button();
			button.x = 100;
			button.y = 150;
			button.label = "Get!";
			button.addEventListener(MouseEvent.MOUSE_DOWN, function doIt():void {
				LastFM.getSimilarArtist(txt.text);
			});
			addChild(button);
			
			//get music from VK
			
		}
		
		
		public function getFlashVarInt( s:String ):int {
			return (vars[s] != null) ? parseInt (vars[s]) : 0;
		}
		public function searchMusic(artist : String) {
			//TODO play music
		}
		
	}
	
}